#include "stdafx.h"
#include <iostream>
#include "indexresource.hpp"
#include "actor.hpp"
#include <sfml/graphics.hpp>
#include <sfml/graphics/sprite.hpp>
#include <pantheios/inserters/real.hpp>
#include <pantheios/inserters/integer.hpp>
#include "testactor.hpp"
#include <box2d.h>
#include "actor_manager.hpp"
#include "message.hpp"
#include <boost/bind.hpp>
#include "mousecontroller.hpp"
#include "game_graph.hpp"
#include "textureManager.hpp"
#include "anim_actor.hpp"
#include "tile_image.hpp"
#include "sound_buffer_manager.hpp"
//#include "pantheios/backends/bec.file.h"
#include "mousecontroller.hpp"
#include "page_property_map.hpp"
#include "sound_manager.hpp"
#include "game_map_mover.hpp"
using namespace FndGame2D ;

void tst_fun(float x, float y) {
    std::cout << x << " " << y << std::endl ;
}

std::map<std::string, sigc::connection> con ;
void test_sig(int i, const std::string& info) {
  pantheios::log_DEBUG(pantheios::integer(i), " I am not dead!!!! ", info, "\n") ;
  if(con.find(info) != con.end()) {
    con[info].disconnect() ;
    con.erase(info) ;
  }
  pantheios::log_DEBUG("hello world\n") ;
}

typedef sigc::signal<void, int> test_sig_type ;


int main( int argc, char** argv ) {
  try {
    page_property_map m_m ;
    m_m.load_info_from_file("info.txt") ;
    m_m.save_info_to_file("info2.txt") ;
    sf::RenderWindow app(sf::VideoMode(640,480,32), "sfml test") ;
    thinker_manager actor_mgr(app) ;
    anim_actor* a = new anim_actor("good") ;
    actor_mgr.handle_message(make_message(a->name(), actor_mgr.name(), 0.0f,
					  actor_mgr.ADD_THINKER, a->baser())) ;
    game_graph* graph = new game_graph("graph") ;
    actor_mgr.handle_message(make_message(graph->name(), actor_mgr.name(),
					  0.0f,
					  actor_mgr.ADD_THINKER, graph->baser()));
    game_map_mover* graph_mover = new game_map_mover(0.15f, "g_mover",1, mover::position_type(100.f, 100.f),
						     0.0f, graph) ;
    game_map_mover* g2 = new game_map_mover(0.15f, "g_mover2", 2, mover::position_type(129.f, 190.f),
					    0.0f, graph) ;
    actor_mgr.handle_message(make_message(g2->name(), actor_mgr.name(),
					  0.f, actor_mgr.ADD_THINKER, g2->baser())) ;
    actor_mgr.handle_message(make_message(graph_mover->name(), actor_mgr.name(),
					  0.f, actor_mgr.ADD_THINKER, graph_mover->baser())) ;
    actor_mgr.handle_message(make_message(graph->name(), graph->name(),
					  0.0f,
					  graph->LOAD_MAP_FROM_FILE, 
					  std::string("game_map.dat"))) ;
    /*
      thinker* mousecontrol = new mouse_controller("mouse control") ;
      actor_mgr.handle_message(make_message(mousecontrol->name(),
      actor_mgr.name(),
      0.0f,
      actor_mgr.ADD_THINKER,
      mousecontrol)) ;
    */
			     
    sf::Image* test = new sf::Image() ;
    if(!test->LoadFromFile("mouse.bmp")) {
      delete test ;
      test = 0 ;
      return 1 ;
    }
    sf::Image* tile_mover_image = new sf::Image() ;
    if(!tile_mover_image->LoadFromFile("tile_mover.bmp")) {
      delete tile_mover_image ;
      tile_mover_image = 0 ;
      return 1 ;
    }
    texture_manager t_mgr ;
    a->push_rect(sf::IntRect(0,0, 16, 16)) ;
    a->push_rect(sf::IntRect(16,16,32,32)) ;
    a->create_anim("default") ;
    a->push_anim_index("default", 0) ;
    a->push_anim_index("default", 1) ;
   
    t_mgr.add_image("mouse", test) ;
    t_mgr.add_image("mover_image", tile_mover_image) ;
    actor_mgr.handle_message(make_message(actor_mgr.name(),
					  graph_mover->name(),
					  0.0f,
					  quad_actor::SET_IMAGE,
					  tile_mover_image)) ;
    actor_mgr.handle_message(make_message(actor_mgr.name(),
					  g2->name(),
					  0.0f,
					  quad_actor::SET_IMAGE,
					  tile_mover_image)) ;
    actor_mgr.handle_message(make_message(actor_mgr.name(),
					  g2->name(),
					  0.0f,
					  quad_actor::SET_RECT,
					  sf::IntRect(0,0,20,20))) ;

    sf::Image* tile_i = new sf::Image() ;
    if(!tile_i->LoadFromFile("tile_rect.bmp")) {
      delete tile_i ;
      return 1 ;
    }
    t_mgr.add_image("tile_image", tile_i) ;
    actor_mgr.handle_message(make_message(a->name(), a->name(), 0.0f,
					  a->SET_IMAGE, t_mgr.get_image("mouse"))) ;
    actor_mgr.handle_message(make_message(a->name(), a->name(), 0.0f,
					  a->SET_ANIM_LOOP, true)) ;
    actor_mgr.handle_message(make_message(a->name(), a->name(), 0.0f,
					  a->SET_ANIM_SPEED, 0.33f)) ;
    a->set_anim("default") ; 
    tile_image t_image("t_image") ;
    t_image.read_rects_from_file("tile_rect.dat") ;
    actor_mgr.handle_message(make_message(graph->name(),graph->name(),
					  0.0f, graph->SET_TILE_IMAGE, &t_image)) ;
    normal_thinker_message_type m_s("",graph->name(),0.0f, graph->SET_BACK_IMAGE,tile_i) ;
    actor_mgr.handle_message(m_s) ;

    sf::Clock clock ;
    while(app.IsOpened()) {
      sf::Event event ;
      while(app.GetEvent(event)) {
	if(event.Type == sf::Event::Closed) {
	  app.Close() ;
	} else if(event.Type == sf::Event::KeyPressed) {
	  actor_mgr.handle_message(make_message(a->name(), a->name(),
						0.0f, a->SET_ANIM_LOOP,
						false)) ;
	  sf::FloatRect rect = a->bounding_box() ;
	  std::cout << rect.Left << " " << rect.Right << " " << rect.Bottom << std::endl ;
	}
      }
    
      actor_mgr.update(clock.GetElapsedTime()) ;
      clock.Reset() ;
      app.SetActive() ;
      app.Clear(sf::Color(128,128,128)) ;

      actor_mgr.render(app) ;

      app.Display() ;
    }
  } catch(const std::exception& e) {
    pantheios::log_ERROR("Exception: ", e) ;
    return 1 ;
  } catch(...) {
    pantheios::logputs(pantheios::emergency, "Unexpected unknow error") ;
    return 1 ;
  }
  return EXIT_SUCCESS ;
}
